/**
 * @author   Ivan Andonov
 * @email:   ivan.andonov[at]gmail[dot]com
 * 
 * @require  sjs, sjs.Delegate, sjs.Utils, jquery
 * @use      sjs.Debug
 * @optional 
 **/

(function() {
	sjs.create('sjs.Form', {

		initialize : function(form) {
			//$log(this+' form='+form);
			this.form = (typeof(form) == 'string') ? document.forms[form] : form;
			
			this.elementsCount = function() {
				return this.form.elements.length;
			};
			
			this.element = function(num) {
				return this.form.elements[num];
			};
			
			this.preserveFieldsValues = function() {
				//$log('preserveFieldsValues form='+form);
				var inputs = this.form.getElementsByTagName('input');
				for (var i in inputs) {
					switch (inputs[i].type) {
						case 'text':
							inputs[i].onfocus = $d(sjs.Form, 'onInputFocus', inputs[i], inputs[i].value);
							inputs[i].onblur = $d(sjs.Form, 'onInputBlur', inputs[i], inputs[i].value);
							break;
					}
				}
				var areas = this.form.getElementsByTagName('textarea');
				for (var i in areas) {
					areas[i].onfocus = $d(sjs.Form, 'onInputFocus', areas[i], areas[i].value);
					areas[i].onblur = $d(sjs.Form, 'onInputBlur', areas[i], areas[i].value);
				}
			};
		},
		
		onInputFocus : function(input, value) {
			//$log(this+'.onInputFocus input='+input+' value='+value);
			if (input.value == value) {
				input.value = '';
			}
		},
		
		onInputBlur : function(input, value) {
			//$log(this+'.onInputBlur input='+input+' value='+value);
			if (!input.value.length) {
				input.value = value;
			}
		},
		
		initOverLabels : function() {
			//$log('initOverLabels');
			if (!document.getElementById) {
				return;
			}
			
			var labels, id, field;
			
			// Set focus and blur handlers to hide and show labels with 'overlabel' class names.
			labels = document.getElementsByTagName('label');
			for (var i = 0; i < labels.length; i++) {
				if (labels[i].className == 'overlabel') {
					// Skip labels that do not have a named association with another field.
					id = labels[i].htmlFor || labels[i].getAttribute('for');
					field = document.getElementById(id);
					if (!id || !field) {
						continue;
					}
					this.initOverLabel(labels[i]);
				}
			}
			
		},
		
		initOverLabel : function(label) {
			//$log('initOverLabel label='+label);
			var hideLabel = function(field_id, hide) {
				var field_for;
				var labels = document.getElementsByTagName('label');
				for (var i = 0; i < labels.length; i++) {
					field_for = labels[i].htmlFor || labels[i].getAttribute('for');
					if (field_for == field_id) {
						labels[i].style.textIndent = (hide) ? '-10000px' : '0px';
						return true;
					}
				}
			};
			
			// Skip labels that do not have a named association with another field.
			id = label.htmlFor || label.getAttribute('for');
			field = document.getElementById(id);
			
			// Change the applied class to hover the label over the form field.
			label.className = 'overlabel_apply';
			
			// Hide any fields having an initial value.
			if (field.value !== '') {
				hideLabel(field.getAttribute('id'), true);
			}
			
			// Set handlers to show and hide labels.
			field.onfocus = function () {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function () {
				if (this.value === '') {
					hideLabel(this.getAttribute('id'), false);
				}
			};
			
			// Handle clicks to LABEL elements (for Safari).
			label.onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) {
					field.focus();
				}
			};
			
		}
		
	});
	
})();